package com.jjh.back.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.servlet.BaseServlet;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.EncryptUtil;
import com.jjh.users.pojo.AAdmins;
import com.jjh.users.pojo.AAdminsEnum;
import com.jjh.users.pojo.ALog;
import com.jjh.users.pojo.ALogEnum;
import com.jjh.users.service.IAuthDbService;
import com.jjh.users.service.IUsersDbService;
import com.jjh.users.service.impl.AuthDbServiceImpl;
import com.jjh.users.service.impl.UsersDbServiceImpl;

/**
 * 后台 不需要登陆就可以访问的Servlet
 * 
 * @author Zjx
 */
@Controller
@RequestMapping
public class NoLoginBackServlet extends BaseServlet
{
	/**
	 * Service方法
	 */
	@Autowired
	private IAuthDbService authDbService ;
	@Autowired
	private IUsersDbService usersDbService ;
	
	
	/**
	 * 加密工具类
	 */
	@Autowired
	private EncryptUtil encryptUtil ;
	

	/**
	 * 登陆提交操作
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/loginSubmit")
	private String loginSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.info = "" ; 
		HttpSession session = request.getSession() ; 
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rand = request.getParameter("rand");
		/* session验证码 */
		String randSess = (String) session.getAttribute("randSess");
		if( (randSess != null && randSess.equalsIgnoreCase(rand)) 
			|| 
			ConstatFinalUtil.RAND_STR.equalsIgnoreCase(rand))
		{
			/* 根据邮箱查询数据库 */
			Map<String,Object> condMap = new HashMap<String,Object>();
			condMap.put("email", email);
			/* 查询扩展信息 */
			condMap.put("extend", "true");
			AAdmins admins = this.usersDbService.findOneAdminsService(condMap);
			if(admins != null)
			{
				/* 方便操作 */
				request.setAttribute("email", email);
				
				/* 邮箱肯定存在 */
				if(this.encryptUtil.checkStr(password,admins.getPassword()))
				{
					/* 密码肯定正确 */
					if(admins.getStatus() == AAdminsEnum.STATUS_ENABLED.getCode())
					{
						/* 状态启用 */
						/* 放到session中 */
						session.setAttribute("lastLoginTime", admins.getLastloginTime());
						session.setAttribute("adminsSess", admins);
						
						/* 修改上次登陆时间 */
						String ip = request.getRemoteAddr();
						admins.setLastLoginIp(ip);
						admins.setLastloginTime(new Date());
						admins.setLoginCount(admins.getLoginCount() + 1 );
						/* 更新用户 */
						this.usersDbService.updateOneAdminsService(admins);
						
						/* 记录日志 */
						ALog log = new ALog();
						log.setAdminsId(admins.getId());
						log.setLogType(ALogEnum.TYPE_LOGIN.getCode());
						log.setIp(ip);
						log.setContent(admins.getEmail() + "----登录系统");
						log.setStatus(ALogEnum.STATUS_ENABLED.getCode());
						
						log.setPubTime(new Date());
						this.usersDbService.saveOneLogService(log);
						
						/* 登陆成功 */
						return "redirect:/back/admin/main" ;
					}else
					{
						/* 状态禁用 */
						this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_STATUS_DISABLED.getCode() + "") ; 
					}
				}else
				{
					/* 密码不正确 */
					this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_PASSWORD_INCORRECT.getCode() + "") ; 
				}
			}else
			{
				/* 邮箱不存在 */
				this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_EMAIL_NOT_EXISTS.getCode() + "") ; 
			}
		}else
		{
			/* 验证码不正确 */
			this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_YANCODE_ERROR.getCode() + "") ; 
		}
		/* 放到request中 */
		request.setAttribute("info", info);
		/* 跳转到登陆页面 */
		return "/back/login" ;
	}

	/**
	 * 打开登陆页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/login")
	private String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("adminsSess");
		if(admins != null)
		{
			/* 已经登陆 */
			return "redirect:/back/admin/main" ;
		}
		/* 跳转到登陆页面 */
		return "/back/login" ;
	}
}
